package com.bci.pwtz.service.impl;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.ZydljdlrefMapper;
import com.bci.pwtz.mysql.model.Zydljdlref;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.ZydljdlrefService;
@Service
public class ZydljdlrefServiceImpl implements ZydljdlrefService
{
    private ZydljdlrefMapper objMapper;
    private BusinessRefService objService;
    private CompanyService service;
    public CompanyService getService()
    {
        return service;
    }

    public void setService(CompanyService service)
    {
        this.service = service;
    }
    
    public BusinessRefService getObjService()
    {
        return objService;
    }

    public void setObjService(BusinessRefService objService)
    {
        this.objService = objService;
    }
    private Logger logger=Logger.getLogger(ZydljdlrefServiceImpl.class);
    
    public ZydljdlrefMapper getObjMapper()
    {
        return objMapper;
    }

    public void setObjMapper(ZydljdlrefMapper objMapper)
    {
        this.objMapper = objMapper;
    }

    @Override
    public Zydljdlref add(Zydljdlref obj) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("add:obj="+obj.toString());
        }
        if(obj==null){
            if(logger.isDebugEnabled()){
                logger.debug("add,obj is null");
            }
            return null;
        }else{
            obj.setLastmodifiedtime(new Date());
            obj.setStatus(1);
        }
        try{
            objMapper.insertSelective(obj);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return obj;
    }

    @Override
    public int update(Zydljdlref obj) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("update:obj="+obj.toString());
        }
        if(obj==null){
            if(logger.isDebugEnabled()){
                logger.debug("update,obj is null");
            }
            return 0;
        }else{
            obj.setLastmodifiedtime(new Date());
        }
        int result=0;
        try{
            result=objMapper.updateByPrimaryKeySelective(obj);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public int delete(String deviceid) throws PwtzDatabaseException, CanNotDeleteException
    {
        if(logger.isDebugEnabled()){
            logger.debug("delete:deviceid="+deviceid);
        }
        int result=0;
        try{
            result=objMapper.deleteById(deviceid, new Date());
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public Zydljdlref load(String deviceid) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("load:deviceid="+deviceid);
        }
        Zydljdlref obj=null;
        try{
            obj=objMapper.selectByPrimaryKey(deviceid);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return obj;
    }

    @Override
    public PaginationList<Zydljdlref> list(String deviceid,int portaltype,String begintime,String endtime,int pagesize,int pageno) throws PwtzDatabaseException
    {
        PaginationList<Zydljdlref> plist=new PaginationList<Zydljdlref>();
        List<Zydljdlref> list=null;
        if(logger.isDebugEnabled()){
            logger.debug("list:|deviceid="+deviceid+"|portaltype="+portaltype+"|begintime="+begintime+"|endtime="+endtime+"|pagesize="+pagesize+"|pageno="+pageno);
        }
        int beginindex=pagesize*pageno-pagesize;
        try{
            list=objMapper.selectListByDl(deviceid,portaltype,begintime,endtime,beginindex,pagesize);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        int totalCount = 0;
        int totalPage =1;
        if(pagesize!=0){
            try
            {
                totalCount = objMapper.selectCountByDl(deviceid,portaltype,begintime,endtime);
            }
            catch (DataAccessException e)
            {
                logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pagesize == 0 ? totalCount / pagesize : totalCount / pagesize + 1;
        }else{
            pageno=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        plist.setTotalPage(totalPage);
        plist.setCurrPage(pageno);
        plist.setPageSize(pagesize);
        plist.setRecords(list);
        plist.setTotalRecord(totalCount);
        return plist;
    }

    @Override
    public boolean isRefExist(String cablepitid, String connectioncable) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("isRefExist:|cablepitid="+cablepitid+"|connectioncable="+connectioncable);
        }
        int result=0;
        try{
            result=objMapper.selectRef(cablepitid, connectioncable);
            if(result>0){
                return false;
            }
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return true;
    }
}
